from fastapi import BackgroundTasks, UploadFile, File, Form, Depends, HTTPException, status
from fastapi_mail import FastMail, MessageSchema, ConnectionConfig
from dotenv import dotenv_values
from pydantic import BaseModel, EmailStr
from typing import List
from models import User
import jwt

config_credentials = dotenv_values(".env")

email_conf = ConnectionConfig(
    MAIL_USERNAME=config_credentials['EMAIL_SENDER'],
    MAIL_PASSWORD=config_credentials['PASS'],
    MAIL_FROM=config_credentials['EMAIL_SENDER'],
    MAIL_SERVER=config_credentials['EMAIL_SERVER'],
    MAIL_PORT=25,
    MAIL_SSL=False,
    MAIL_TLS=True,
    USE_CREDENTIALS=True,
)


# class EmailSchema(BaseModel):
#     email: List[EmailStr]

async def echo(word):
    print(word)


async def send_email(email: List[EmailStr], instance: User):
    token_data = {
        "id": instance.id,
        'username': instance.username,
    }

    token = jwt.encode(token_data, key=config_credentials['TOKEN_SECRET'], algorithm='HS256').decode('utf8')
    html = f"""
            <html lang="en">
            <head>
            </head>
            <body>
                <div sytle='display: flex; align-items: center; justify-content: center; flex-direction: column'>
                    <h3>Account Verification</h3>
                    <br>
                    <p> Thanks for choosing EasyShopas, please click on the button below to verify your account</p>
                    <br>
                    <br>
                    <a style="margin-top: 1rem; padding: 1rem; border-radius: 0.5rem; font-size: 1rem; text-decoration: none; background: #0275d8; color: white;" href="http://192.168.0.211:8081/verification/?token={token}">Verify your email</a>
                    <br>
                    <br>
                    <p>Please kindly ignore this email if you did not register for EasyShopas and nothing will happend. Thanks</p>
                </div>
            </body>
            </html>
            """

    message = MessageSchema(
        subject="Easyshopas Account Verification Email!",
        recipients=email,
        body=html,
        subtype='html'
    )

    fm = FastMail(email_conf)
    await fm.send_message(message=message)
